#include "../StdInc.h"

namespace GSTrader
{
	namespace TradeStrategy
	{
		CMsgDispatch::CMsgDispatch(CFramework* pFramework, bool bQueueThread) : m_pFramework(pFramework), m_bQueueThread(bQueueThread)
		{
			if (m_bQueueThread)
			{
				m_pMsgQueue = new CMsgQueue();
				m_pThread = new thread(bind(&CMsgDispatch::ProcessThread, this));
			}
			else
			{
				m_pMsgQueue = NULL;
				m_pThread = NULL;
			}
		}

		CMsgDispatch::~CMsgDispatch()
		{
			_delete(m_pThread)
			_delete(m_pMsgQueue)
		}

		void CMsgDispatch::ProcessThread()
		{
			while (true)
			{
				CMsg* pMsg;
				while ((pMsg = m_pMsgQueue->Pop()) == nullptr)
				{
					ThreadSleep(1);
				}

				OnDispatch(pMsg);
			}
		}

		void CMsgDispatch::OnNewMsg(CMsg* pMsg)
		{
			if (m_bQueueThread)
			{
				m_pMsgQueue->Push(pMsg);
			}
			else
			{
				OnDispatch(pMsg);
			}
		}

		void CMsgDispatch::OnDispatch(CMsg* pMsg)
		{

		}
	}
}